<?php
/*
 * @Author: KingMin 
 * @Date: 2021-07-13 22:34:59 
 * @Last Modified by:   KingMin 
 * @Last Modified time: 2021-07-13 22:34:59 
 * @The authors website: https://www.kingmin.cn  
 */
/**
 * 配置查询及设置
 * @copyright (c) KINGMIN All Rights Reserved
 */

class Config_Model {
    // 获取配置分类
    public static function getConfigTabList($set=false){
        if($set){
            $showSql="  AND `isshow`='1' AND `status`='0' AND `type`='0' ";
        }else{
            $showSql="";
        }
        $DB=Database::getInstance();
        $sql = "SELECT * FROM `" . DB_PREFIX . "sys_config_tab` WHERE `isdel`='0' AND `pid`='0' $showSql ORDER BY order_num ASC ;";
        $logs = $DB->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            $pid=$v['id'];
            $v['statusT']=$v['status']=='0'?false:true;
            $v['isshowT']=$v['isshow']=='0'?false:true;
            $v['order_num']=intval($v['order_num']);
            $children=self:: getConfigTabChildren($pid,$set);
            $v['children']=$children;
            $data[]=$v;
        }
        return $data;
    }
    
    // 获取配置子菜单分类
    public static function getConfigTabChildren($id='',$set=false){
        if($set){
            $showSql="  AND `isshow`='1' AND `status`='0' AND `type`='0' ";
        }else{
            $showSql="";
        }
        if(!$id){
            return [];
        }
        $DB=Database::getInstance();
        $sql = "SELECT * FROM `" . DB_PREFIX . "sys_config_tab` WHERE `isdel`='0' AND `pid`='$id' $showSql ORDER BY order_num ASC ;";
        $logs = $DB->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            $pid=$v['id'];
            $v['statusT']=$v['status']=='0'?false:true;
            $v['isshowT']=$v['isshow']=='0'?false:true;
            $v['order_num']=intval($v['order_num']);
            $children=self:: getConfigTabChildren($pid,$set);
            if(count($children)>0){
                $v['children']=$children;
            }
            $data[]=$v;
        }
        return $data;
    }
    
    // 获取配置信息
    
    public static function getConfigList($pid){
        if(!$pid){
            return [];
        }
        
        $DB=Database::getInstance();
        
        $sql = "SELECT * FROM `" . DB_PREFIX . "sys_config` WHERE `isdel`='0' AND `config_tab_id`='$pid'  AND `isshow`='0' ORDER BY order_num ASC ;";
        $logs = $DB->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            $v=self::configJson($v);
            $data[$v['fields']]=$v;
        }
        return $data;
    }
    public static function getConfigDetailList($pid){
        if(!$pid){
            return [];
        }
        
        $DB=Database::getInstance();
        
        $sql = "SELECT * FROM `" . DB_PREFIX . "sys_config` WHERE `isdel`='0' AND `config_tab_id`='$pid'  ORDER BY order_num ASC ;";
        $logs = $DB->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            $v=self::configJson($v);
            $data[]=$v;
        }
        return $data;
    }
    public static function getConfigDetail($key){
        if(!$key){
            return false;
        }
        $DB=Database::getInstance();
        
        $res = $DB->query("SELECT * FROM `".DB_PREFIX."sys_config` where `fields`='$key' ");
        $config =false;
        while($row = $DB->fetch_array($res)) {
           $row=self::configJson($row);
            $config=$row;
        }
        return $config;
    }
    public static function uploadConfigDetail($dt,$key){
        $DB=Database::getInstance();
        if(!$key){
            return false;
        }
        if(!is_array($dt)){
            return false;
        }
        
        $rt=true;
        if(count($dt)>0){
            $sql=upSqlConvert('sys_config',$dt,"`fields`='$key'");
            // return $sql;
            $rt=$DB->query($sql);
            $CACHE = Cache::getInstance();
            $CACHE->updateCache('options');
        }
        return $rt;
    }
    public static function uploadConfigTab($dt,$id){
        $DB=Database::getInstance();
        if(!$id){
            return false;
        }
        if(!is_array($dt)){
            return false;
        }
        
        $rt=true;
        if(count($dt)>0){
            $sql=upSqlConvert('sys_config_tab',$dt,"`id`='$id'");
            // return $sql;
            $rt=$DB->query($sql);
        }
        return $rt;
    }
    // 新增
    public static function addConfigTab($dt=[]){
        $DB=Database::getInstance();
        $fields=self::getConfigTabKey(@$dt['fields'])??true;
        if($fields){
            return false;
        }
        
        $sql=addSqlConvert('sys_config_tab',$dt);
        // return $sql;
        $rt=true;
        $rt=$DB->query($sql);
        $id=$DB->insert_id();
        return $rt;
    }
    // 新增
    public static function addConfig($dt=[]){
        $DB=Database::getInstance();
        $fields=self::getConfigDetail(@$dt['fields'])??true;
        if($fields){
            return false;
        }
        
        $sql=addSqlConvert('sys_config',$dt);
        // return $sql;
        $rt=true;
        $rt=$DB->query($sql);
        $id=$DB->insert_id();
        $CACHE = Cache::getInstance();
        $CACHE->updateCache('options');
        return $rt;
    }
    public static function getConfigTab($id){
        if(!$id){
            return false;
        }
        $DB=Database::getInstance();
        
        $res = $DB->query("SELECT * FROM `".DB_PREFIX."sys_config_tab` where `id`='$id' ");
        $config =false;
        while($row = $DB->fetch_array($res)) {
            $row['statusT']=$row['status']=='0'?false:true;
            $row['isshowT']=$row['isshow']=='0'?false:true;
            $row['order_num']=intval($row['order_num']);
            $config=$row;
        }
        return $config;
    }
    public static function getConfigTabKey($key){
        if(!$key){
            return false;
        }
        $DB=Database::getInstance();
        
        $res = $DB->query("SELECT * FROM `".DB_PREFIX."sys_config_tab` where `fields`='$key' ");
        $config =false;
        while($row = $DB->fetch_array($res)) {
            $row['statusT']=$row['status']=='0'?false:true;
            $row['isshowT']=$row['isshow']=='0'?false:true;
            $row['order_num']=intval($row['order_num']);
            $config=$row;
        }
        return $config;
    }
     // 用户格式化
    public static function configJson($config=false){
        if(!$config){
            return false;
        }
        $ifs=false;
        if($config['required']){
            $required=explode(",",$config['required']);
            $rd=[];
            foreach($required as $k => $d ){
                $n=explode(":",$d);
                if($n[0]=='if'){
                    $ifs['if']=$n[1];
                }
                if($n[0]=='ifv'){
                    $ifs['ifv']=$n[1];
                }
                if($n[0]=='min'||$n[0]=='max'){
                    $n[1]=intval($n[1]);
                }
                $nl=[];
                @$nl[$n[0]]=isBoolStr($n[1]);
                $rd[]=$nl;
            }
            $config['requiredList']=$rd;
        }
        $config['if']=$ifs;
        if($config['type']=='radio'||$config['type']=='checkbox'||$config['type']=='json'){
            $parameter=explode("\n",$config['parameter']);
             $rd=[];
            foreach($parameter as $k => $d ){
                $n=explode("=>",$d);
                $nl=[];
                $rd[$n[0]]=isBoolStr($n[1]);
                // $rd=$nl;
                // array_push($rd,$nl);
            }
            $config['parameterList']=$rd;
        }
        if($config['type']=='checkbox'){
            $config['value']=json_decode($config['value'],true);
            if(!$config['value']){
                $config['value']=[];
            }
        }
        if($config['type']=='json'){
            $config['value']=json_decode($config['value'],true);
            if(!$config['value']){
                $config['value']=[];
            }
        }
        if($config['type']=='upload'){
            if($config['upload_type']=='1'){
                $config['value']=imgUrlpd($config['value']);
            }elseif($config['upload_type']=='2'){
                $config['value']=json_decode($config['value'],true);
                if($config['value']){
                    foreach($config['value'] as $i => $d){
                        $config['value'][$i]=imgUrlpd($d);
                    }
                }else{
                    $config['value']=[];
                }
            }
        }
        if($config['input_type']=='number'){
            $config['value']=intval($config['value']);
        }
        $config['order_num']=intval($config['order_num']);
        $config['high']=intval($config['high']);
        $config['isshowT']=$config['isshow']=='1'?false:true;
        return $config;
    }
    
}